﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using HandyControl.Controls;
using CommunityToolkit.Mvvm.Input;
using Ops.Host.App.Utils;
using Ops.Host.Common;
using Ops.Host.Core.Models;
using Ops.Host.Core.Services;

namespace Ops.Host.App.ViewModels;

public sealed class CardInfoViewModel : AsyncSinglePagedViewModelBase<CardInfo, CardInfoFilter>
{
    private readonly ICardInfoService _cardInfoService;

    public CardInfoViewModel(ICardInfoService cardInfoService)
    {
        _cardInfoService = cardInfoService;

        AddCommand = new RelayCommand(() => Add());
        UpdateCommand = new RelayCommand<CardInfo>((item) => Update(item!));
        DeleteCommand = new RelayCommand<CardInfo>((item) => Delete(item!));
        SaveCommand = new RelayCommand(async () => await SaveAsync());

        _ = InitSearchAsync();
    }

    #region 绑定属性

    private List<NameValue> _levelList = DropdownListHelper.FromEnum<CardInfoLevel>();
    public List<NameValue> LevelList
    {
        get => _levelList;
        set => SetProperty(ref _levelList, value);
    }

    private CardInfo? _cardInfo0;
    public CardInfo? CardInfo0
    {
        get => _cardInfo0;
        set => SetProperty(ref _cardInfo0, value);
    }

    private bool _isOpen = false;
    public bool IsOpen
    {
        get => _isOpen;
        set => SetProperty(ref _isOpen, value);
    }

    private bool _isAdding = false;
    public bool IsAdding
    {
        get => _isAdding;
        set => SetProperty(ref _isAdding, value);
    }

    #endregion

    #region 绑定事件

    public ICommand AddCommand { get; }

    public ICommand UpdateCommand { get; }

    public ICommand DeleteCommand { get; }

    public ICommand SaveCommand { get; }

    #endregion

    #region privates

    public void Add()
    {
        CardInfo0 = new();

        IsAdding = true;
        IsOpen = true;
    }

    public void Update(CardInfo item)
    {
        CardInfo0 = item;

        IsAdding = false;
        IsOpen = true;
    }

    public void Delete(CardInfo item)
    {
        Growl.Ask("确定是否要删除？", isConfirmed =>
        {
            if (isConfirmed)
            {
                _cardInfoService.DeleteCardInfo(item.Id);
                DataSourceList?.Remove(item);
                Growl.Info("数据删除成功");
            }
            return true;
        });
    }

    public async Task SaveAsync()
    {
        var cardInfo = CardInfo0;
        if (cardInfo!.PLevel == null)
        {
            Growl.Info("权限等级不能为空");
            return;
        }

        // 更新
        if (!cardInfo!.IsTransient())
        {
            await _cardInfoService.UpdateCardInfoAsync(cardInfo);

            IsOpen = false;
            Growl.Info("数据更新成功");
            return;
        }

        // 新增
        if (string.IsNullOrWhiteSpace(cardInfo!.CardNo))
        {
            Growl.Info("卡号不能为空");
            return;
        }
        
        if (_cardInfoService.Exist(cardInfo!.CardNo))
        {
            Growl.Info($"卡号 {cardInfo!.CardNo} 已存在");
            return;
        }

        var cardInfo0 = await _cardInfoService.AddCardInfoAsync(cardInfo);
        DataSourceList!.Add(cardInfo0);
      
        IsOpen = false;
        Growl.Info("数据新增成功");
    }

    protected override async Task<(IEnumerable<CardInfo> items, long pageCount)> OnSearchAsync(int pageIndex, int pageSize)
    {
        return await _cardInfoService.GetPagedAsync(QueryFilter, pageIndex, pageSize);
    }

    #endregion
}
